package com.shop.bookstore.service;

import com.shop.bookstore.dao.ReaderDao;

import com.shop.bookstore.entity.Reader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReaderService implements ReaderDao {
    //    实现操作方法增删改查
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int add(Reader reader) {
        String sql = "insert into reader values(null,?,?,?,?)";
//        调用方法实现
        Object[] args = {reader.getReadername(),reader.getIdCard(),reader.getBorrow_books(),reader.getTimeout_record()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int delete(Reader reader) {
        String sql = "delete from reader where Id=?";
        Object[] args={reader.getId()};
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public int update(Reader reader) {
        String sql = "update reader set readername=?,idCard=?,borrow_books=?,timeout_record=? where Id=?";
        Object[] args={
                reader.getReadername(),reader.getIdCard(),reader.getBorrow_books(),reader.getTimeout_record(),
                reader.getId()
        };
        return jdbcTemplate.update(sql, args);

    }

    @Override
    public Reader findOne(Reader reader) {
        String sql = "select * from reader where Id="+reader.getId().toString();
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Reader>(Reader.class));
    }

    @Override
    public List<Reader> findAll() {
        String sql = "select * from reader";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Reader>(Reader.class));

    }
}
